﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Rest;
using Facebook.Web.FbmlControls;
using Facebook.Web;

public partial class Message : LutefiskPage
{
    //Creates messages to send to friends
    protected void spamFriends(Object source, EventArgs e)
    {
        string APIKey = "816569bc7dc6ff9717d81dde78321615";    //Define the key secret
        string APISecret = "52091cb1b4969b8f7f9e5b100764b297"; //Define the APISecret

        //create a connection to facebook
        Facebook.Session.ConnectSession connectsession = new Facebook.Session.ConnectSession(APIKey, APISecret);
        Facebook.Rest.Api api = new Facebook.Rest.Api(connectsession);

        //create an attachment
        Facebook.Rest.attachment_media_image myimage = new Facebook.Rest.attachment_media_image();
        
        //Link for image
        myimage.href = "http://apps.facebook.com/lutefisk";
        myimage.src = "http://www.facebookmusicians.smartsiteonline.net/Images/lutefisk.png";

        //make attachement
        List<Facebook.Rest.attachment_media> mymedia = new List<Facebook.Rest.attachment_media>();
        mymedia.Add(myimage);
                
        Facebook.Rest.attachment myattachment = new Facebook.Rest.attachment();

        //Put the announcement here
        myattachment.caption = messageBox.Text;

        //description
        myattachment.description = "";

        //same something stupid to get user attention
        myattachment.name = "LUTEFISK SAYS: ";

        //some kind of link ...read the API
        myattachment.href = "";

        //make the attachment
        myattachment.media = mymedia;

        IList<Facebook.Schema.action_link> myactionlinks = new List<Facebook.Schema.action_link>();
        Facebook.Schema.action_link myactionlink = new Facebook.Schema.action_link();
        myactionlink.href = "http://www.mydomain.com/mypage";
        myactionlink.text = "Take an action";
        myactionlinks.Add(myactionlink);
        
        try
        {
            api.Stream.Publish("", myattachment, myactionlinks, "", getUID());
        }
        catch (Exception ex) 
        { 
            //If you get here, something horrible happened. Give up on life.
        }
    }

    //get the current users ID
    private long getUID()
    {
        CanvasFBMLMasterPage master = (CanvasFBMLMasterPage)this.Master;
        string uid = master.Api.Users.GetInfo().uid.ToString();

        return Int64.Parse(uid);
    }
}